#include "LevelManager.h"



LevelManager::LevelManager( )
{

}


LevelManager::~LevelManager( )
{

}



void LevelManager::initWithFile( const char* levelXmlFile )
{
	std::string filePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(levelXmlFile);

	xml_document doc;
	xml_parse_result result = doc.load_file( filePath.c_str() );

	if( !result )
		return;

	xml_node pRoot = doc.child("map");

	//orbitindicator
	xml_node pOrbitIndicator = pRoot.child("orbitindicator");
	std::vector<float> listRadius;

	for( xml_node indicator = pOrbitIndicator.child("indicator") ; indicator ; indicator = indicator.next_sibling() )
	{
		float radius = atof( indicator.attribute("radius").value() );
		listRadius.push_back(radius);
	}

	LStaticArray<CircleOrbitInfo>* pListCircleOrbit = LStaticArray<CircleOrbitInfo>::create();

	//Circle orbit
	for( xml_node circleOrbitNode = pRoot.child("circleorbit") ; circleOrbitNode ; circleOrbitNode = circleOrbitNode.next_sibling("circleorbit") )
	{
		CircleOrbitInfo* pCircleOrbitInfo = new CircleOrbitInfo();
		pCircleOrbitInfo->setSpeed( atof( circleOrbitNode.attribute("speed").value() ) );
		
		for( xml_node circleNode = circleOrbitNode.child("circle") ; circleNode ; circleNode = circleNode.next_sibling("circle") )
		{
			CircleInfo* pCircleInfo = new CircleInfo();
			pCircleInfo->setAngle( atof( circleNode.attribute("angle").value() ) );
			for( xml_node circleSegmentNode = circleNode.child("circlesegment") ; circleSegmentNode ; circleSegmentNode = circleSegmentNode.next_sibling("circlesegment") )
			{
				CircleSegmentInfo* pSegmentInfo = new CircleSegmentInfo( );
				pSegmentInfo->setType( atoi( circleSegmentNode.attribute("type").value() ) );
				pSegmentInfo->setColorType( atoi( circleSegmentNode.attribute("colortype").value() ) );
				pSegmentInfo->setRadius( atof( circleSegmentNode.attribute("radius").value() ) );

				pCircleInfo->getListSegment()->addObject( pSegmentInfo );
			}
			pCircleOrbitInfo->getListCircle()->addObject( pCircleInfo );
		}

		pListCircleOrbit->addObject( pCircleOrbitInfo );
	}
	
	int size = pListCircleOrbit->count();
	int size2 , size3;
	int i ,  j , k;
	OrbitManager* pOrbitManager = OrbitManager::sharedOrbitManager();
	pOrbitManager->initWithNumOrbitSystem( size, ORBIT_NUM_SHOW , &listRadius );


	for( i=0 ; i < size ; ++i )
	{
		OrbitSystem* pOrbitSystem = new OrbitSystem();
		pOrbitSystem->initWithRadius( 1000 );

		pOrbitManager->addOrbitSystem( pOrbitSystem );
		pOrbitSystem->release( );

		CircleOrbitInfo* pCircleOrbitInfo = pListCircleOrbit->getObject(i);

		size2 = pCircleOrbitInfo->getListCircle()->count();

		for( j = 0 ; j < size2 ; ++j )
		{
			Circle* pCircle = new Circle( );
			pCircle->init( );
			pCircle->setSpeed( pCircleOrbitInfo->getSpeed() );
			pCircle->rotate();

			pOrbitSystem->addCircle( pCircle  );
			pCircle->release();

			CircleInfo* pCircleInfo = pCircleOrbitInfo->getListCircle()->getObject(j);
			size3 = pCircleInfo->getListSegment()->count();

			for( k = 0 ; k < size3 ; ++k )
			{
				CircleSegmentInfo* pSegmentInfo = pCircleInfo->getListSegment()->getObject(k);
				CircleSegment* pSegment = new CircleSegment( );
				pSegment->initWithType( (SEGMENT_TYPE)pSegmentInfo->getType() , pSegmentInfo->getRadius() , (COLOR_TYPE)pSegmentInfo->getColorType() );
				pCircle->addSegment( pSegment , pCircleInfo->getAngle() );
				pSegment->release();
			}
		}
	}


	CC_SAFE_DELETE( pListCircleOrbit );
}